<?php
set_time_limit(0); 
ini_set('memory_limit','256M');

require_once("./config.php");
header("Content-Type: text/html;charset=utf-8");
function test_post($url,$post_data)
{
	//初始化
    $ch = curl_init();
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
	curl_setopt($ch, CURLOPT_TIMEOUT, 5);
	$resultJson = curl_exec($ch);
	$curlInfo = curl_getinfo($ch);
	$errNo = curl_errno($ch);
	curl_close($ch);
    //显示获得的数据
	return $resultJson;
    //var_dump($resultJson,$curlInfo,$errNo);
}

function parse($content){

	$table_data = preg_match_all('/<tbody[^>]*>(.*?) <\/tbody>/si',$content,$match);
	$table_data = $match[0][0];
	$tr_data = explode("<tr>",$table_data);
	$data =array();
	for($i=1;$i<count($tr_data);$i++){
		$data[$i] = explode('</td>',$tr_data[$i]);
		for($j = 0;$j<count($data[$i]);$j++){
			$data[$i][$j] = preg_replace('/\s(?=\s)/','',trim(strip_tags($data[$i][$j])));
		}
		array_pop($data[$i]);
	}
	return $data;
}

function mysqlconn()
{
	$mysql_server_name="localhost"; //数据库服务器名称
    $mysql_username="root"; // 连接数据库用户名
    $mysql_password=""; // 连接数据库密码
    $mysql_database="chsi"; // 数据库的名字
    // 连接到数据库
    $conn=mysqli_connect($mysql_server_name, $mysql_username,
                        $mysql_password);
	if (mysqli_connect_errno($conn)) 
	{ 
		echo "连接 MySQL 失败: " . mysqli_connect_error(); 
	} 
	mysqli_query($conn,"use ".$mysql_database);
	mysqli_query($conn,"SET NAMES utf8");

	return $conn;
}
function mysqlclose($conn){
	mysqli_close($conn);
}


function fectch($sql,$conn){

	$redata =array();
	if ($result=mysqli_query($conn,$sql))
	{
		// 一条条获取
		$i=0;
		while ($row=mysqli_fetch_assoc($result))
		{
			$redata[$i]=$row;
			$i++;
		}
		// 释放结果集合
		mysqli_free_result($result);
		return $redata;
	}
	return false;
}

function mksearch()
{
	$conn = mysqlconn();


	$sql = "SELECT mc as zymc,searchkey as  yjxkdm FROM chsi_major WHERE LEVEL =3 ";
	//$sql = "SELECT mc as zymc,searchkey as  yjxkdm FROM chsi_major WHERE LEVEL =3  limit 3";

	$data = fectch($sql,$conn);

	foreach($data as $key=>$val){

		$sql = "SELECT *  FROM chsi_major WHERE dm='".$val['yjxkdm']."'";
		$rs = fectch($sql,$conn);
		$data[$key]['mldm'] = $rs[0]['searchkey'];
	}
	mysqlclose($conn);
	return $data;
}
function mkurl($ssdm,$list)
{
	//$searckey = array();
	$xxfs[0] =1;
	$xxfs[1] =2;

	$ssdm = json_decode($ssdm,true);
	$i = 0;
	foreach($ssdm as $key=>$val){
		$str ="";
			foreach($xxfs as $k=>$v){

				foreach($list as $lk=>$lv){
					$tt = array();
					/*
					$searckey[$i]['ssdm'] = $val['dm'];
					$searckey[$i]['dwmc'] = '';
					$searckey[$i]['mldm'] = $lv['mldm'];
					$searckey[$i]['mlmc'] = '';
					$searckey[$i]['yjxkdm'] = $lv['yjxkdm'];
					$searckey[$i]['zymc'] = $lv['zymc'];
					$searckey[$i]['xxfs'] = $v;
					$str.=json_encode($searckey[$i]).";\n";
					*/
					//$i++;
					$tt['ssdm'] = $val['dm'];
					$tt['dwmc'] = '';
					$tt['mldm'] = $lv['mldm'];
					$tt['mlmc'] = '';
					$tt['yjxkdm'] = $lv['yjxkdm'];
					$tt['zymc'] = $lv['zymc'];
					$tt['xxfs'] = $v;
					$str.=json_encode($tt)."\n";


				}

			}
		$filename = 'D:/workdata/test/chsi/se/'.$val['dm'].".txt";
		$fh=fopen($filename, "w+");
		fwrite($fh, $str);    // 输出：6
		fclose($fh);
		echo $filename."<br>";

			//break;
	}
	//return $searckey;
}

$list = mksearch();
$rs = mkurl($ssdm,$list);

//var_dump($rs);


// 专业查询
$url="https://yz.chsi.com.cn/zsml/queryAction.do";
$data['ssdm'] = 12;
$data['dwmc'] = '';
$data['mldm'] = 'zyxw';
$data['mlmc'] = '';
$data['yjxkdm'] = '0252';
$data['zymc'] = '应用统计';
$data['xxfs'] = '';

/*理论查询

ssdm: 11
dwmc: 
mldm: 02
mlmc: 
yjxkdm: 0201
zymc: 理论经济学
xxfs: 
*/


//$html = test_post($url,$data);
//print_r(json_decode($ssdm,true));








?>